#设置对应label的内容
from stressPrinter.Model import Globalvariables


def find_matching_info(view_select,matchStation):
    labelGroup = []
    for i in range(len(view_select)):
        labelContent = []
        if view_select[i] == 'CD':
            for m in matchStation:
                labelContent.append(Globalvariables.STATION_CD[m])
            labelGroup.append(labelContent)
        if view_select[i] == 'BH':
            for m in matchStation:
                labelContent.append(Globalvariables.STATION_BH[m])
            labelGroup.append(labelContent)
        if view_select[i] == 'KS':
            for m in matchStation:
                labelContent.append(Globalvariables.STATION_KS[m])
            labelGroup.append(labelContent)
        if view_select[i] == 'CSY':
            for m in matchStation:
                labelContent.append(Globalvariables.STATION_CSY[m])
            labelGroup.append(labelContent)
    return labelGroup

def find_matching_indices():    #查找匹配的下标
    matchStation = []
    try:
        for l in range(len(Globalvariables.STATION)):
            i = Globalvariables.STATION_dict.index(Globalvariables.STATION[l])
            matchStation.append(i)
    except ValueError:
        raise ValueError("台站不存在！")
    return matchStation
def splic_label(labelGroup):
    # 检查输入列表是否为空
    if not labelGroup:
        return []

    length = len(labelGroup[0])
    result = []
    for i in range(length):
        label_string = ','.join(sublist[i] for sublist in labelGroup)
        result.append(label_string)

    return result

def initLabel():
    matchStation = find_matching_indices()
    labelGroup = find_matching_info(Globalvariables.VIEW_SELECT,matchStation)
    labelGroup = splic_label(labelGroup)
    return labelGroup
    #print(labelGroup)
    #print(Globalvariables.VIEW_SELECT)

def initLabel_1(label):
    vs = ['CD','BH','KS','CSY']
    Globalvariables.STATION = []
    result = []
    matchStation = []
    for i in label:
        Globalvariables.STATION.append(i.replace("-", "_"))
    try:
        matchStation = find_matching_indices()
    except ValueError as e:
        print(str(e))

    if len(matchStation) == len(label):
        labelGroup = find_matching_info(vs, matchStation)
        labelGroup = splic_label(labelGroup)
        for i in range(len(label)):
            result.append(label[i] + ',' + labelGroup[i])
        return result
    else:
        return label